package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bo.CategortUpdateBO;
import com.cskaoyan.module.pojo.MarketCategory;
import com.cskaoyan.module.pojo.MarketCategoryExample;
import com.cskaoyan.module.systemVO.CategoryFatherListVO;
import com.cskaoyan.module.systemVO.CategoryL1ListVO;
import com.cskaoyan.module.systemVO.CategoryL1VO;
import com.cskaoyan.module.systemVO.CategoryListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Author: 周佳豪
 * @Date: 2022/04/08/10:22
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    MarketCategoryMapper categoryMapper;

    BaseRespVo baseRespVo = new BaseRespVo();

    @Override
    public BaseRespVo categoryList() {
        //先查询所有L1的商品类目
        List<CategoryL1VO> list = categoryMapper.selectL1("L1");
        //一级类目id
        List<Integer> listFId = new LinkedList<>();
        //new返回对象
        CategoryListVO categoryListVO = new CategoryListVO();

        List<CategoryFatherListVO> lists = new LinkedList<>();

        List<MarketCategory> children = new LinkedList<>();

        for (CategoryL1VO categoryL1VO : list) {
            listFId.add(categoryL1VO.getValue());
        }
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(listFId);
        //一级类目
        List<MarketCategory> marketCategoriesF = categoryMapper.selectByExample(example);
        for (MarketCategory c : marketCategoriesF) {
            CategoryFatherListVO categoryFatherListVO = new CategoryFatherListVO();
            categoryFatherListVO.setId(c.getId());
            categoryFatherListVO.setName(c.getName());
            categoryFatherListVO.setDesc(c.getDesc());
            categoryFatherListVO.setLevel(c.getLevel());
            categoryFatherListVO.setIconUrl(c.getIconUrl());
            categoryFatherListVO.setKeywords(c.getKeywords());
            categoryFatherListVO.setPicUrl(c.getPicUrl());

            MarketCategoryExample exampleSon = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteriaSon = exampleSon.createCriteria();
            criteriaSon.andPidEqualTo(c.getId());
            children = categoryMapper.selectByExample(exampleSon);
            categoryFatherListVO.setChildren(children);
            lists.add(categoryFatherListVO);
        }
        categoryListVO.setList(lists);
        categoryListVO.setLimit(lists.size());
        categoryListVO.setTotal(lists.size());
        categoryListVO.setPage(1);
        categoryListVO.setPages(1);
        baseRespVo.setData(categoryListVO);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @Override
    public BaseRespVo categoryCreate(MarketCategory marketCategory) {
        marketCategory.setAddTime(new Date());
        marketCategory.setUpdateTime(new Date());
        marketCategory.setDeleted(false);
        int insert = categoryMapper.insert(marketCategory);
        baseRespVo.setErrno(0);
        baseRespVo.setData(marketCategory);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @Override
    public BaseRespVo categoryL1() {
        List<CategoryL1VO> list = categoryMapper.selectL1("L1");
        CategoryL1ListVO categoryL1ListVO = new CategoryL1ListVO(list.size(), 1, list.size(), 1, list);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(categoryL1ListVO);
        return baseRespVo;
    }

    @Override
    public BaseRespVo categoryUpdate(CategortUpdateBO c) {
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(c.getId());
        marketCategory.setName(c.getName());
        marketCategory.setKeywords(c.getKeywords());
        marketCategory.setDesc(c.getDesc());
        marketCategory.setIconUrl(c.getIconUrl());
        marketCategory.setPicUrl(c.getPicUrl());
        marketCategory.setLevel(c.getLevel());
        marketCategory.setPid(c.getPid());
        int i = categoryMapper.updateByPrimaryKeySelective(marketCategory);
        List<MarketCategory> children = c.getChildren();
        if (children != null) {
            for (MarketCategory child : children) {
                i = categoryMapper.updateByPrimaryKeySelective(child);
            }
        }
        if (i == 1) {
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
        }
        return baseRespVo;
    }

    @Override
    public BaseRespVo categoryDelete(CategortUpdateBO categortUpdateBO) {
        int i = categoryMapper.deleteByPrimaryKey(categortUpdateBO.getId());
        if (categortUpdateBO.getChildren() != null) {
            List<MarketCategory> children = categortUpdateBO.getChildren();
            for (MarketCategory c : children) {
                i = categoryMapper.deleteByPrimaryKey(c.getId());
            }
        }
        if (i == 1) {
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
        }
        return baseRespVo;
    }
}
